import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
import math
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

root = ET.parse("D:\\StarCraft\\bwapi-data\\logs\\g.xml").getroot()
generations = root.findall("generation")

avg_w = [[],[],[],[],[],[]]

def bit2double(bit,top_val):
	bitmax = pow(2,len(bit))
	val = int(bit,2)
	return (val/float(bitmax)) * top_val

for gen in generations:
	for o in avg_w: o.append(0)
	for g in gen.findall("genome"):
		i = 0
		bitstring = g.findtext("bitstring")

		avg_w[0][len(avg_w[0])-1] += bit2double(bitstring[:7],50)
		avg_w[1][len(avg_w[1])-1] += bit2double(bitstring[7:19],50)
		avg_w[2][len(avg_w[2])-1] += bit2double(bitstring[19:25],50)
		avg_w[3][len(avg_w[3])-1] += bit2double(bitstring[25:35],50)
		avg_w[4][len(avg_w[4])-1] += bit2double(bitstring[35:46],50)
		avg_w[5][len(avg_w[5])-1] += bit2double(bitstring[46:55],50)
		
	for avg in avg_w:
		avg[len(avg)-1] = avg[len(avg)-1] / float(len(gen.findall("genome")))
		
plt.ylabel("generations")
plt.subplot(241).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 1")
plt.plot(avg_w[0])
plt.subplot(242).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 2")
plt.plot(avg_w[1])
plt.subplot(243).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 3")
plt.plot(avg_w[2])
plt.subplot(244).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 4")
plt.plot(avg_w[3])
plt.subplot(245).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 5")
plt.plot(avg_w[4])
plt.subplot(246).xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.title("weight 6")
plt.plot(avg_w[5])
plt.show()